//
//  LoginCheckUtil.m
//  HaoRenSnack
//
//  Created by shaowei on 13-8-2.
//  Copyright (c) 2013年 LianZhan. All rights reserved.
//

#import "LoginCheckUtil.h"
#import "SWCommonMacro.h"

@implementation LoginCheckUtil

//封装正则验证过程
+ (NSInteger)generateRegularWithPattern:(NSString *)pattern andString:(NSString *)string{
    if (!string) {
        return 0;
    }
    
    NSError *error;
    NSRegularExpression *regularTmp = [NSRegularExpression regularExpressionWithPattern:pattern
                                                                                options:NSRegularExpressionCaseInsensitive
                                                                                  error:&error];
    NSInteger resultOfNumber = [regularTmp numberOfMatchesInString:string
                                                           options:0
                                                             range:NSMakeRange(0, [string length])];
    return resultOfNumber;
}

static BOOL sIsCanPopErrorAlert = NO;

+ (void)setPopErrorAlertEnable:(BOOL)enable{
    sIsCanPopErrorAlert = enable;
}

+ (BOOL)canPopErrorAlertEnable{
    return sIsCanPopErrorAlert;
}

//展示警告信息
+ (void)showAlertTitle:(NSString *)title content:(NSString *)content{
    if (sIsCanPopErrorAlert) {
        ALERT_VIEW(title, content);
    }
    
}

#pragma mark - Login-Check

+ (BOOL)checkEmail:(NSString *)email{
    //1.验证用户名格式
    NSInteger resultOfUserName = [LoginCheckUtil generateRegularWithPattern:REGULAR_MAIL
                                                                  andString:email];
    if (!resultOfUserName) {
        [LoginCheckUtil showAlertTitle:@"用户名格式错误" content:@"用户名为邮箱格式，请重新输入！"];
        return NO;
    }
    
    return YES;
}

+ (BOOL)checkPassword:(NSString *)pwd{
    //验证密码格式
    NSInteger resultOfPassword = [LoginCheckUtil generateRegularWithPattern:REGULAR_PASSWORD
                                                                  andString:pwd];
    if (!resultOfPassword) {
        [LoginCheckUtil showAlertTitle:@"密码格式错误" content:@"密码6～20位，不能包含特殊字符"];
        return NO;
    }
    
    return YES;
}

+ (BOOL)checkPhoneNumber:(NSString *)phone{
    //验证手机号
    NSInteger resultOfPassword = [LoginCheckUtil generateRegularWithPattern:REGULAR_PHONE
                                                                  andString:phone];
    if (!resultOfPassword) {
        [LoginCheckUtil showAlertTitle:@"手机号格式错误" content:@"手机号为13、15、18开头的11位数字"];
        return NO;
    }
    
    return YES;
}

+ (BOOL)checkOAuthCode:(NSString *)oauthCode{
    //验证验证码
    NSInteger resultOfPassword = [LoginCheckUtil generateRegularWithPattern:REGULAR_OAUTHCODE
                                                                  andString:oauthCode];
    if (!resultOfPassword) {
        [LoginCheckUtil showAlertTitle:@"验证码格式错误" content:@"验证码为6位纯数字"];
        return NO;
    }
    
    return YES;
}

+ (BOOL)checkNickname:(NSString *)name{
    //验证昵称 ^.{1,15}$
    BOOL result = NO;
    if (name.length > 0 && name.length <= 15) {
        result = YES;
    }
    
    if (!result) {
        [LoginCheckUtil showAlertTitle:@"用户名格式错误" content:@"用户名为1～15个字符"];
        return NO;
    }
    
    return YES;
}



@end
